package com.zyl.ag.ui.cal;

import android.app.Activity;
import android.app.AlertDialog;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.zyl.ag.R;
import com.zyl.ag.SysApplication;
import com.zyl.ag.bean.AgTD;
import com.zyl.ag.ui.widget.WidgetProvider;
import com.zyl.util.CalUtil;
import com.zyl.util.SharedPreferencesUtil;
import com.zyl.util.UrlUtil;

public class CalActivity  extends Activity {

	final Context context = this;

	private int mAppWidgetId;
	private Button okButton;
	private RadioButton radioButton1,radioButton2;
	private EditText editText1, editText2,editText3, editText4;
	private  TextView currentPrice,textView1;
	TextView mTitleView;
	 RadioGroup group;


	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.cal_activity);
		 String str=UrlUtil.getAgTD();
		 AgTD ag=AgTD.getAgTD(str);  
		
		
		mTitleView = (TextView) findViewById(R.id.title_text);
		currentPrice = (TextView) findViewById(R.id.currentPrice);
		textView1 = (TextView) findViewById(R.id.textView1);
		editText1 = (EditText) findViewById(R.id.editText1);
		editText2 = (EditText) findViewById(R.id.editText2);
		editText3 = (EditText) findViewById(R.id.editText3);
		editText4 = (EditText) findViewById(R.id.editText4);
		radioButton1= (RadioButton) findViewById(R.id.radioButton1);
		radioButton2= (RadioButton) findViewById(R.id.radioButton2);
	    group = (RadioGroup)this.findViewById(R.id.radioGroup);
	    okButton = (Button) findViewById(R.id.ok_Button);

	    mTitleView.setText("计算器");
	    currentPrice.setText(ag.getPrice() );
	    radioButton1.setChecked(true);
		editText1.setText("15");
		editText3.setText(ag.getPrice().subSequence(0, ag.getPrice().indexOf(".")));
		editText4.setText(ag.getPrice().subSequence(0, ag.getPrice().indexOf(".")));
		
		
		okButton.setOnClickListener(okClickListener);
	}
	 OnClickListener okClickListener=new OnClickListener() {
		public void onClick(View arg0) {
			 
			try {
				double fee=Double.parseDouble(editText1.getText().toString());
				int n=Integer.parseInt(editText2.getText().toString());

				double sign=1;
				if(group.getCheckedRadioButtonId()==R.id.radioButton2) sign=-1;
				double open=Double.parseDouble(editText3.getText().toString());
				double close=Double.parseDouble(editText4.getText().toString()); 
				textView1.setText(CalUtil.agtd(  sign,   open,  close,  n,  fee)+"");
			} catch (NumberFormatException e) {
				
			}
			
			
		}
	};
	public void clear(View arg0) {
		 
	 
		textView1.setText( "");
		editText3.setText( "");
		editText4.setText( "");
		
	}
	
	public void refresh(View arg0) {
		String str = UrlUtil.getAgTD();
		AgTD ag = AgTD.getAgTD(str);
		currentPrice.setText(ag.getPrice());
		Toast.makeText(context, "已刷新", Toast.LENGTH_SHORT);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
		 
			 ShowExitdialog() ;
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	protected void ShowExitdialog() 
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(CalActivity.this);

		builder.setMessage("确定要退出吗?");

		builder.setTitle("提示");

		builder.setPositiveButton("确认",
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
					 
			            
						dialog.dismiss();
						 SysApplication.getInstance().exit();
						//HomeActivity.this.finish();
					}

				});

		builder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						dialog.dismiss();

					}

				});

		builder.create().show();

	}
}
